package cn.edu.huas.shopping.service;

import cn.edu.huas.shopping.dao.BannerDao;
import cn.edu.huas.shopping.dao.BannerDaoImpl;
import cn.edu.huas.shopping.dao.ProductDao;
import cn.edu.huas.shopping.dao.ProductDaoImpl;
import cn.edu.huas.shopping.exception.DaoException;
import cn.edu.huas.shopping.pojo.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductServiceImpl implements ProductService {

    private ProductDao productDao=new ProductDaoImpl();
    private BannerDao bannerDao=new BannerDaoImpl();

    /*Map<String, List<IndexProduct>>*/
    @Override
    public Map<String, Object> loadIndexProduct() throws DaoException {
        //获得最新商品
        List<IndexProduct> newsProduct=productDao.queryIndexProduct("0");
        //获得推荐商品
        List<IndexProduct> recommendProduct=productDao.queryIndexProduct("1");
        //获得轮播图信息
        List<Banner> banners=bannerDao.queryBanner();
        //封装成Map集合
        /*Map<String,List<IndexProduct>> maps=new HashMap<>();*/
        Map<String,Object> maps=new HashMap<>();
        maps.put("0",newsProduct);
        maps.put("1",recommendProduct);
        maps.put("2",banners);
        return maps;
    }

    @Override
    public Product loadOneProduct(Integer id) throws DaoException {
        return productDao.queryById(id);
    }

    @Override
    public OrderItem loadOrderItem(Integer id) throws DaoException {
        return  productDao.queryOrderItem(id);
    }

    @Override
    public PageResult<Product> pageQuery(Integer cid, Integer pageIndex, Integer pageSize) throws DaoException {
        //1.计算总版页数
        int total=productDao.queryCountByCid(cid);
        int pageTotal=total%pageSize==0?total/pageSize:total/pageSize+1;
        //2.获得分页数据
        int beginIndex=(pageIndex-1)*pageSize;
        List<Product> products=productDao.queryProductsByCid(cid,beginIndex,pageSize);

        PageResult<Product> pageResult=new PageResult<>();
        pageResult.setPageTotal(pageTotal);
        pageResult.setPageSize(pageSize);
        pageResult.setPageIndex(pageIndex);
        pageResult.setData(products);

        return pageResult;
    }


}
